<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">
            <el-dialog :title="title" :visible.sync="dialogFormVisible">
                <el-form :model="addForm">
                    <el-form-item label="机构名" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="简介" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.intro" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="领导" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.lead" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="机构设置" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.institutionSetting" auto-complete="off"></el-input>
                    </el-form-item>

                    <el-form-item label="机构分类" :label-width="formLabelWidth">
                        <el-select v-model="addForm.organizationType" placeholder="请选择机构分类">
                            <el-option v-for="(type,index) in organizationTypes" :key="index" :label="type" :value="index+1"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="机构类型" :label-width="formLabelWidth">
                        <el-select v-model="addForm.type" placeholder="请选择机构类型">
                            <el-option v-for="(type,index) in types" :key="index" :label="type" :value="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="区域" :label-width="formLabelWidth">
                        <el-select v-model="addForm.territoryName" placeholder="请选择所选区域">
                            <el-option v-for="(territory,index) in territorys" :key="index" :label="territory.name" :value="territory.name"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="联系我们" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.contactUs" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="索引" prop="" :label-width="formLabelWidth">
                        <el-input v-model="addForm.sortIndex" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="是否隐藏" :label-width="formLabelWidth">
                        <el-switch v-model="show" active-color="#13ce66">
                        </el-switch>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="add">确 定</el-button>
                </div>
            </el-dialog>
            <el-input v-model="select_word" placeholder="输入机构名" @click="search" class="search"></el-input>
            <el-select v-model="organizationType" placeholder="请选择机构类型">
                <el-option v-for="(type,index) in organizationTypes" :key="index" :label="type" :value="index+1"></el-option>
            </el-select>
            <el-button type="primary" icon="search" @click="search">搜索</el-button>
            <el-button type="primary" @click="addEx">新增机构</el-button>
        </div>
        <div>
            <el-table :data="page.content" border fit style="width: 100%">
                <el-table-column prop="name" label="机构名" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="intro" label="简介" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="lead" label="领导" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="institutionSetting" label="机构设置" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="type" :formatter="formatArea" label="机构类型" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="organizationType" :formatter="formatType" label="机构类型" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="territoryName" label="区域" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="contactUs" label="联系我们" show-overflow-tooltip>
                </el-table-column>

                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" @click="editOrg(scope.row)" size="small">编辑</el-button>
                        <el-button type="text" @click="delOrg(scope.row)" size="small">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="block">
            <el-pagination @current-change="handleCurrentChange" :current-page="page.number+1" layout="total, prev, pager, next, jumper" :total="page.totalElements">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import { allOrganizations, putOrganization, delOrganization, addOrganization, territory } from "../../server/getDate.js";
import { mapState, mapActions } from 'vuex'
import { getStore } from '../../utils/local.js'
import { confirm } from '../../utils/local'
export default {
    data() {
        return {
            value: '',
            loading: false,
            dialogTableVisible: false,
            dialogFormVisible: false,
            select_word: '',
            addForm: {

            },
            formLabelWidth: '120px',
            title: '',
            types: ['智库机构', '智库联盟'],
            //organizationTypes: ['大学', '党校', '政研室', '科研机构'],
            organizationTypes: ['党政智库', '社科院智库', '党校（行政学院）智库', '省级专门智库', '省级改革智库', '高校智库', '社会智库'],
            show: false,
            organizationType: ''
        }
    },
    created() {
        this.getOrg()
        this.getArea()
        console.log('区域', this.territorys)
    },
    computed: mapState({
        page: state => state.Org.allOrgs,
        territorys: state => state.Org.territorys,
        user: state => state.Login.user
    }),
    methods: {
        ...mapActions(['allOrgs', 'allTerritorys']),
        search() {
            this.loading = true
            let index = this.page.number + 1
            this.handleCurrentChange(index)
        },
        getArea() {
            territory()
                .then(res => {
                    if (res.status === 0) {
                        this.allTerritorys(res.result)
                    }
                })
        },
        formatArea(row, col) {
            if (row.type == 1) {
                return '智库联盟'
            } else {
                return '智库机构'
            }
        },
        formatType(row) {
            // if (row.organizationType == 1) {
            // return '大学'
            // } else {
            // return '政府'
            // }
            switch (row.organizationType) {
                case 1:
                    return '党政智库'
                case 2:
                    return '社科院智库'
                case 3:
                    return '党校（行政学院）智库'
                case 4:
                    return '省级专门智库'
                case 5:
                    return '省级改革智库'
                case 6:
                    return '高校智库'
                case 7:
                    return '社会智库'
            }
        },
        handleClick(row) {
            console.log(row)
        },
        formatRole(row, col) {
            let roles = this.roles
            for (let i in roles) {
                if (roles[i].id === row.roleId) {
                    return roles[i].name
                }
            }
            return '未知'
        },
        getOrg() {
            allOrganizations()
                .then(res => {
                    if (res.status === 0) {
                        this.allOrgs(res.result)
                    }
                })
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            let field = 'name'
            let keyword = this.select_word
            let organizationType = this.organizationType
            console.log('afdsa', organizationType)
            let index = val
            let size = this.page.size
            allOrganizations({ field, keyword, index, size, organizationType })
                .then(res => {
                    this.loading = false
                    if (res.status === 0) {
                        this.allOrgs(res.result)
                    } else {
                        this.$message.error(res.message)
                    }
                }, err => this.$message.error('网络异常'))
        },
        delOrg(row) {
            this.$confirm('此操作将永久删除该机构, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let userId = this.user.id
                let id = row.id
                delOrganization({ id, userId })
                    .then(res => {
                        console.log('删除机构', res)
                        if (res.status === 0) {
                            this.getOrg()
                            this.$message.info('删除成功')
                        } else {
                            this.$message.error(res.message)
                        }
                    }, err => this.$message.error('服务器异常'))
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        add() {
            this.title = '添加机构'
            let organizationEntity = this.addForm
            organizationEntity.shield = this.show ? 1 : 0
            let userId = this.user.id
            if (organizationEntity.id) {
                putOrganization({ organizationEntity, userId })
                    .then(res => {
                        if (res.status === 0) {
                            this.dialogFormVisible = false
                            this.$message.info('修改成功')

                        } else {
                            this.$message.error(res.message)
                        }
                    }, err => this.$message.error('服务器异常'))
            } else {
                addOrganization({ organizationEntity, userId })
                    .then(res => {
                        console.log('机构', res)
                        if (res.status === 0) {
                            this.dialogFormVisible = false
                            this.$message.info('添加成功')
                            this.getOrg()
                        } else {
                            this.$message.error(res.message)
                        }
                    }, err => this.$message.error('服务器异常'))
            }
        },
        addEx() {
            this.title = '添加机构'
            this.dialogFormVisible = true
            this.addForm = {}
        },
        editOrg(row) {
            console.log('修改', row)
            this.dialogFormVisible = true
            this.title = '修改机构'
            this.addForm = row
            this.show = row.shield ? true : false
        },
    }
}
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
}

.block {
  text-align: center;
  margin-top: 1%;
}
</style>
